package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCProductDao;

    @Autowired
    private ContractDao contractDao;
    /*
     附件分页
     */
    @Override
    public PageInfo<ExtCproduct> findByPage(ExtCproductExample extCProductExample, int pageNum, int pageSize) {
        //设置开始页与页面大小
        PageHelper.startPage(pageNum,pageSize);
        //查询全部
        List<ExtCproduct> extCProductList = extCProductDao.selectByExample(extCProductExample);
        PageInfo<ExtCproduct> pageInfo = new PageInfo<>(extCProductList);
        return pageInfo;
    }

    //查询全部
    @Override
    public List<ExtCproduct> findAll(ExtCproductExample extCProductExample) {
        List<ExtCproduct> extCProductList = extCProductDao.selectByExample(extCProductExample);
        return extCProductList;
    }

    //根据主键查询
    @Override
    public ExtCproduct findById(String id) {
        return extCProductDao.selectByPrimaryKey(id);
    }

    // 保存附件
    @Override
    public void save(ExtCproduct extCProduct) {
        extCProduct.setId(UUID.randomUUID().toString());
        //附件创建时间
        extCProduct.setCreateTime(new Date());
        //附件的更新时间
        extCProduct.setUpdateTime(new Date());

        //1. 计算出附件的总价
        double amount = 0;
        if (extCProduct.getPrice()!=null && extCProduct.getCnumber()!=null){
            amount = extCProduct.getPrice() * extCProduct.getCnumber();
            extCProduct.setAmount(amount);
        }
        //2. 插入附件数据
        extCProductDao.insertSelective(extCProduct);
        //3.修改购销合同的总价
        Contract contract = contractDao.selectByPrimaryKey(extCProduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //4. 修改购销合同的附件种类数量
        if (contract.getExtNum()!=null){
            contract.setExtNum(contract.getExtNum() + 1);
        }else {
            contract.setExtNum(1);
        }

        //5. 更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);

    }

    //更新附件
    @Override
    public void update(ExtCproduct extCProduct) {
        ExtCproduct oldExtCproduct = extCProductDao.selectByPrimaryKey(extCProduct.getId());

        //附件的更新时间
        extCProduct.setUpdateTime(new Date());
        //1. 计算附件的总价
        double amount = 0;
        if (extCProduct.getPrice()!=null && extCProduct.getCnumber()!=null){
            amount = extCProduct.getPrice() * extCProduct.getCnumber();
            extCProduct.setAmount(amount);
        }
        //2. 更新附件
        extCProductDao.updateByPrimaryKeySelective(extCProduct);
        //3. 重新计算购销合同总价
        Contract contract = contractDao.selectByPrimaryKey(extCProduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() - oldExtCproduct.getAmount() + amount);
        //4. 更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    //删除附件
    @Override
    public void delete(String id) {
        //1. 根据id找附件
        ExtCproduct dbExtCproduct = extCProductDao.selectByPrimaryKey(id);

        //2. 删除附件
        extCProductDao.deleteByPrimaryKey(id);

        //3. 更新购销合同总价
        Contract contract = contractDao.selectByPrimaryKey(dbExtCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() - dbExtCproduct.getAmount());

        //4. 更新购销合同种类
        contract.setExtNum(contract.getExtNum() - 1);
        //5. 更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

}
